package com.java.oop;

import java.util.Arrays;

public class ArrayTools {
    public static void sort(int[] array){
        //外层循环控制比较次数
        for (int i = 0; i < array.length-1; i++) {
            //内层循环控制比较次数
            for (int j = 0; j < array.length-1-i; j++) {
                //比较相邻元素，大的交换到后面
                if (array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }

    public static int BinarySearch(int[] array,int num){
        int start = 0;
        int end = array.length-1;
        while(start <= end){
            int mid = (start+end)/2;
            if (num > array[mid]){
                start = mid+1;
            } else if (num < array[mid]) {
                end = mid-1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }
    public static boolean equal(int[] arr1,int[] arr2){
        if (arr1.length == arr2.length){
            for (int i = 0; i < arr1.length; i++) {
                if (arr1[i] == arr2[i]){
                    continue;
                }else {
                    return false;
                }
            }
            return true;

        }else return false;

    }

    public static void main(String[] args) {

        int[] a = {10,20,30,40,50,60};
        int[] b = {40,10,20,30,50,60};
        ArrayTools.sort(b);
        //输出排序后的b数组
        System.out.println(Arrays.toString(b));
        //40用二分查找的位置
        System.out.println(ArrayTools.BinarySearch(b,40));
        //判断两个数组是否相等
        boolean result = ArrayTools.equal(a,b);
        System.out.println(result);
    }
}
